<template>
  <div class="wrapper">
    <minbar title="领券中心" :isPadding="true" @rightButtonClick="openMy" :rightButton="true">
        <text slot="right" class="right-title" >我的</text>
    </minbar>
    <!-- 顶部的tab页 -->
    <x-toptab :tabTitles="tabTitles" @itemClick="tabItemClick"></x-toptab>
    <x-list 
      class="x-list" 
      ref="clist"
      :config="configData" 
      :param="listparam" 
      @receiveData="receiveData" >
      <div slot="content">
        <x-listpage :listData="listData" :ctype="4" @buttonClick="buttonClick"></x-listpage>
      </div>
    </x-list>
    <div v-if="nodata" class="x-nodata"> 
      <text class="iconfont x-nodata-icon">&#xe679;</text>
      <text class="x-nodata-font">没有可用的优惠券~</text>
    </div>
  </div>
</template>

<script>
import asCore from "../../../common/js/core";
const navigator = weex.requireModule('navigator');
export default { 
  components: {
    "minbar" : require("../../../common/component/minbar.vue"),
    "x-toptab" : require("../../../common/component/stoptab.vue"),
    "x-listpage" : require("./comp/couponPage.vue"),
    "x-list": require("../../../common/component/list.vue")
  },
  created() {
    this.loadStore();
  },
  data() {
    return {
      nodata:false,
      currentid:null,
      tabTitles:[],
      configData:{
        pageRow:{
          page:1,
          limit:10
        },
        action:"/coupon/list"
      },
      listData:[],
      listparam:{store_id:66666}
    }
  },
  methods: {
    /* 加载门店数据 */
    loadStore(){
      asCore.post("/member/store/list",{},(data,msg,flag)=>{
        if(data&&data['length']>0){
          for(var i in data){
            this.tabTitles.push({
              title:data[i]['name'],
              code:data[i]['id']
            }); 
          }
          this.tabItemClick(data[0]['id'],0);
        }else{
          //未关注门店的逻辑
        }

      });
    },
    tabItemClick(code,index){
        this.currentid = code;
        this.listparam.store_id = code;
        this.$nextTick(()=>{
          this.$refs.clist.refreshData();
        });
    },
    receiveData(type,data){ 
      if(!this.currentid) return;
      //asCore.alert(JSON.stringify(data));
      if(type=="refresh"){
        for(var i in data){
          data[i]['isreceive'] = 0;
        }
        this.listData = data;
        this.nodata = false; 
        if(!data || data['length']==0){
          this.nodata = true;
        }
      }else{
        this.listData.push(...data);
      }
    },
    /*领取优惠券 */
    buttonClick(type,data,index){
      if(type==4&&data['isreceive']==0){
        asCore.post("/coupon/receive",{
          coupon_id:data['id']
        },(data,msg,flag)=>{
          flag ? 
          (this.listData[index]['isreceive'] = 1):asCore.toast(msg);
        });
      }
      //asCore.alert(JSON.stringify(data));
    },
    openMy(){
      navigator.back();
    }
  }
};
</script>
<style scoped>
.iconfont {
    font-family:iconfont;
}
.wrapper {
  background-color: #dedfe1;
}
.right-title {
  color: #fff;
  font-size: 32px;
}
.x-nodata {
  position:absolute;
  top: 190;
  bottom: 0;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  
}
.x-nodata-icon {
  margin-top:-200;
  font-size:200px;
  color:#a1a1a1;
}
.x-nodata-font {
  font-size:30px;
  color:#a1a1a1;
  text-align: center;
  width: 750px;
}

</style>

